/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;

using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Activation;

using ELE.EnterpriseLibrary.Aspects.Properties;
using ELE.EnterpriseLibrary.Naming;

namespace ELE.EnterpriseLibrary.Aspects.Attributes
{

  /// <summary>
  /// Context-attribute that is used to add a context-property, namely, Aspects.Properties.LogProperty into the 
  /// target context during the object activation phase of a context-bound object's lifecycle.
  /// </summary>
  [AttributeUsage(AttributeTargets.Class)]
  public class AspectAttribute : Attribute, IContextAttribute
  {
    private string mConfigFile = null;
    public const string CTX_KEY = "ctx:";
    /// <summary>
    /// No arg constructor
    /// </summary>
    public AspectAttribute() { }
    /// <summary>
    /// Creates the attribute based on a config file
    /// </summary>
    /// <param name="configFile"></param>
    public AspectAttribute(string configFile)
    {
      if (configFile != null)
      {

        if (configFile.StartsWith(CTX_KEY))
        {
          configFile = configFile.Remove(0, CTX_KEY.Length);
          int div = configFile.IndexOf(":");
          string rootDir = null;
          if (div == 0)
          {
            INamingContext ctx = NamingContextFactory.CreateContext();
            if (ctx != null)
            {
              rootDir = (string)ctx.Environment[NamingContextConstants.LOCAL_CONTEXT_ROOT];
            }
          }
          else
          {
            string ctxName = configFile.Substring(0, div);
            INamingContext ctx = NamingContextFactory.CreateContext(ctxName);
            if (ctx != null)
            {
              rootDir = (string)ctx.Environment[NamingContextConstants.LOCAL_CONTEXT_ROOT];
            }
          }
          if (rootDir != null)
          {
            if (!rootDir.EndsWith(@"\"))
            {
              rootDir += @"\";
            }
            configFile = rootDir + configFile.Substring(div + 1, configFile.Length - div - 1);
          }
          else
          {
            configFile = null;
          }
        }
        if (configFile != null && System.IO.File.Exists(configFile))
        {
          mConfigFile = configFile;
        }
      }

    }
    /// <summary>
    /// Gets the properties
    /// </summary>
    /// <param name="msg"></param>
    public void GetPropertiesForNewContext(IConstructionCallMessage msg)
    {
      AspectProperty property = new AspectProperty(mConfigFile);
      msg.ContextProperties.Add(property);
    }
    /// <summary>
    /// Implementation of interface method
    /// </summary>
    /// <param name="ctx"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public bool IsContextOK(Context ctx, IConstructionCallMessage msg)
    {
      return false;
    }
  }
}
